﻿	var stop_t=false;//暂停游戏；
	var stop_e;//暂停监听；
	var sA=[];//蛇；
	var speed=150;//初始速度；
	var score=0;//分数；
	var moveCode=null;
	var interval;//移动和食物计时器；
	var speedTime;//加速计时器；
	var food=null;//食物；
	var startgo=false;//暂停提速；
	
	//定义方法
	function $(x){
		return document.getElementById(x);
	}
	
	//键盘事件
	function keydown(e){
		switch(e.keyCode){
			case 32:
			//case 90:
			//case 122:
				e.preventDefault()
				stop_t=!stop_t;
				break;
			case 38:
			case 87:
		    case 119:
			case 39:
			case 68:
			case 100:
			case 40:
			case 83:
			case 115:
			case 37:
			case 65:
			case 97:
				moveCode=e.keyCode;
				break;
			default:
				return;
		}
		if(stop_t){
		    $('second').style.left=0;
			clearInterval(interval);
			$('audio_bg').pause();
			$('nav_').style.top='-500px';
		}
		else{
			$('second').style.left='500px';
			$('audio_bg').play();
			clearInterval(interval);
			interval=setInterval(function(){move()},speed);
			if(moveCode){
				startgo=true;
			}
			$('nav_').style.top=0;
		}
	}
	
	function move(){
		var x1,y1;
		var v=sA[1].id.split('_');
		v[0]=parseInt(v[0]);
		v[1]=parseInt(v[1]);
		switch(moveCode){
			case 38:
			case 87:
		    case 119:
				if(v[0]!=getXY('x')-1){
				   x1=getXY('x')-1;
				   }
				else{
					x1=getXY('x')+1;
				}
				y1=getXY('y');
				break;
			case 39:
			case 68:
			case 100:
				x1=getXY('x');
				if(v[1]!=getXY('y')+1){
					y1=getXY('y')+1;
				}
				else{
					y1=getXY('y')-1;
				}
				
				break;
			case 40:
			case 83:
			case 115:
				if(v[0]!=getXY('x')+1){
					x1=getXY('x')+1;
				}
				else{
					x1=getXY('x')-1;
				}
				y1=getXY('y');
				break;
			case 37:
			case 65:
			case 97:
				x1=getXY('x');
				if(v[1]!=getXY('y')-1){
					y1=getXY('y')-1;
				}
				else{
					y1=getXY('y')+1;
				}	
				break;
			default:
				return;
		}
		detection(x1,y1);
	}
	
	//移动和检测是否碰撞
	function detection(x,y){
		if(x<0 || x>49 || y<0 || y>49){
		    endStart('撞 墙');
		}
		else if(eatSelf(x,y)){
		    endStart('咬到了自己');
		}
		else if($(x+'_'+y)==food){
			if(sA.length==10 || sA.length==20){
				$('audio_all').src='baby.mp3';
				$('audio_all').play();
			}
			else if(sA.length==50 && sA.length==75){
				$('audio_all').src='young.mp3';
				$('audio_all').play();
			}
			else if(sA.length==100){
				$('audio_all').src='old.mp3';
				$('audio_all').play();
			}
			
			sA.unshift($(x+'_'+y));
			$('score').innerHTML=(score+=1);
			initFood();
		}
		else{
			win();
		    sA.unshift($(x+'_'+y));
		    for(var i=0;i<sA.length-2;i++){
			  sA[i].style.background=sA[i+1].style.background;
			  sA[i].style.boxShadow=sA[i+1].style.boxShadow;
		  }
		  var v=sA.pop().id;
		  $(v).style.background='';
		  $(v).style.boxShadow='';
		}
	}
	
	//判断是否胜利；
	function win(){
		if(sA.length==1000)
			endStart('胜 利！');
	}
	
	//判断是否咬到自己；
	function eatSelf(a,b){
		var v=a+'_'+b;
		for(var i=0;i<sA.length;i++){
			if($(v)==sA[i]){
				return true;
			}	
		}
		return false;
	}
	
	//结束；
	function endStart(a){
		$('audio_bg').pause();
		document.getElementsByTagName('h1')[2].innerHTML=a;
		document.getElementsByTagName('h2')[0].innerHTML='成绩：'+score+'<br>'+'速度：'+speed;
		clearInterval(interval);
		clearInterval(speedTime);
		document.removeEventListener('keydown',keydown);
		food.style.background='';
		food.style.boxShadow='';
		$('third').style.left=0;
		$('info_in').style.left='-110px';
		$('nav_').style.top='-500px';
	}
	
	//获取头坐标
	function getXY(i){
		switch(i){
			case 'x':
				return parseInt(sA[0].id.split('_')[0]);
			case 'y':
				return parseInt(sA[0].id.split('_')[1]);
		}
	}
	
				
	
	//方格地图
	function map(){
	  if(!$('table_map')){
		var game_table=document.createElement('table');
		game_table.setAttribute('id','table_map');
		game_table.setAttribute('cellspacing','0');
		game_table.setAttribute('cellpadding','0');
		var head_width=$('head').clientWidth;
		var head_height=$('head').clientHeight;
		for(var i=0;i<(head_width/10);i++){
			var x=document.createElement('tr');
			for(var j=0;j<(head_height/10);j++){
				var y=document.createElement('td');
				y.id=i+'_'+j;
				x.appendChild(y);
			}
			game_table.appendChild(x);
		}
		  
		$('forth').appendChild(game_table);
	  }
	}
	
	//初始化蛇身
	function Snake_body(){
		if(sA){
			for(var i=0;i<sA.length;i++){
				sA[i].style.background='';
				sA[i].style.boxShadow='';
			}
		}
		var x=document.getElementsByTagName('td');
		var snake=5;
		for(var i=0;i<snake;i++){
			sA[i]=x[x.length/2+25+((-parseInt(snake/2)+i)*50)];
			sA[i].style.background=
				'rgb('+Math.floor(Math.random()*254)+','
			    +Math.floor(Math.random()*254)+','
			    +Math.floor(Math.random()*254)+')';
			sA[i].style.boxShadow='1px 1px 3px black';
		}
	}
	
	//初始化食物
	function initFood(){
		var v1=Math.floor(Math.random()*50);
		var v2=Math.floor(Math.random()*50);
		var v3=v1+'_'+v2;
		if($(v3).style.background==''){
			$(v3).style.background=
				"rgb("+Math.floor(Math.random()*254)+","
				+Math.floor(Math.random()*254)+","
				+Math.floor(Math.random()*254)+")";
			$(v3).style.boxShadow='1px 1px 3px black';
			food=$(v3);
		}
	}
	


	
	
		$('b1').addEventListener('click',freedom_model,false);
		$('b2').addEventListener('click',card_model,false);
		$('b3').addEventListener('click',challenge_model,false);
		$('b4').addEventListener('click',removeB,false);
		$('b5').addEventListener('click',body_A,false);
		
		//兼容手机操作
		$('st1').addEventListener('click',st11,false);
		$('st2').addEventListener('click',st22,false);
		$('st3').addEventListener('click',st33,false);
		$('st4').addEventListener('click',st44,false);
		
		function st11(){this.keyCode=38;keydown(this)}
		function st22(){this.keyCode=37;keydown(this)}
		function st33(){this.keyCode=39;keydown(this)}
		function st44(){this.keyCode=40;keydown(this)}
		
		
		
		//重置游戏
		function removeB(){
			for(var i=0;i<sA.length;i++){
				sA[i].style.background='';
				sA[i].style.boxShadow='';
			}
			if(clearInterval(interval));
			if(clearInterval(speedTime));
		    if(document.removeEventListener('keydown',keydown));
		    if(food.style.background='');
			if(food.style.boxShadow='');
		    $('third').style.left=0;
			score=0;
			speed=150;
			sA.length=5;
			startgo=false;
			moveCode=null;
			food=null;
			$('speed').innerHTML=150;
	        $('score').innerHTML=0;
			var re=$('forth');
			re.removeChild(re.childNodes[0]);
			if(this.id=='b4'){
				freedom_model();
			}
		}
		
		function body_A(){
			removeB();
			$('third').style.left='500px';
			$('first').style.left=0;
		}
		
		//自由模式
		function freedom_model(){
			$('first').style.left='-500px';
			$('third').style.left='500px';
			$('count_down').style.left=0;
			biginAll($('count_down'),3,function(){
				map();
				$('nav_').style.top=0;
			    $('info_in').style.left=0;
			    $('speed').innerHTML=speed;
			    $('audio_bg').play();
				initFood();
				Snake_body();
				document.addEventListener('keydown',keydown,false);
				speedTime=setInterval(function(){
				 if(startgo){
				   if(speed>20){
                    speed-=5;
                    $("speed").innerHTML=speed;
                   }
				   else{
                    window.clearInterval(speedTime);
                   }
				 }
                },5000);
			});
			
		}
		
		//自由模式加载函数
		function biginAll(a,b,c){
			if(b>0){
				a.childNodes[1].innerHTML=b;
					$('audio_all').src='ab.mp3';
				    $('audio_all').play();
				b--;
				setTimeout(function(){biginAll(a,b,c)},1000);
			}
			else{
				a.style.left='-500px';
				c();
			}
		}
		
		
		
		function card_model(){
			var i=true;
                        $('first').style.left='-500px';
			var v=setInterval(function(){
				if(i){
					$('new_two').style.left=0;
					i=!i;
				}
				else{
					$('new_two').style.left='500px';
					$('first').style.left=0;
					clearInterval(v);

					return;
				}
			},1500);
		}
		
		function challenge_model(){
		    var i=true;
		    $('first').style.left='-500px';
		    var v=setInterval(function(){
				if(i){
					$('new_two').style.left=0;
					i=!i;
				}
				else{
					$('new_two').style.left='500px';
					$('first').style.left=0;
					clearInterval(v);
					return;
				}
			},1500);
		}